﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SiteParser.Core
{
	public class HtmlNavigationExpression
	{
		private IDictionary<string, MatchingRule> attributesFilter;
		private MatchingRule tagMatchingRule;

		/// <summary>
		/// Expression of parent element.
		/// </summary>
		public HtmlNavigationExpression ParentExpression { get; set; }

		/// <summary>
		/// Html tag name
		/// </summary>
		public MatchingRule TagMatchingRule {
			get {
				if (tagMatchingRule == null) {
					tagMatchingRule = new MatchingRule();
				}
				return tagMatchingRule;
			}
			set {
				tagMatchingRule = value;
			}
		}

		/// <summary>
		/// Values of attributes which selecting html node should contain.
		/// </summary>
		public IDictionary<string, MatchingRule> AttributesFilter { 
			get {
				if (attributesFilter == null)
				{
					attributesFilter = new Dictionary<string, MatchingRule>();
				}
				return attributesFilter;
			}
			set {
				attributesFilter = value;
			}
		}

		/// <summary>
		/// Specifies inner html or attribute value should be selected as result
		/// </summary>
		public SelectTypes SelectType { get; set; }

		/// <summary>
		/// Used when 'AttributeValue' of SelectType is chosen. Specifiec the attribute name for selecting.
		/// </summary>
		public string SelectingAttributeName { get; set; }

		/// <summary>
		/// Used when required to select predefined value
		/// </summary>
		public string SelectConstantValue { get; set; }

		/// <summary>
		/// Regex of a marker. Used when we doesn't have separate container for some entity.
		/// </summary>
		public string MarkerValue { get; set; }

		/// <summary>
		/// Regex for selecting items in case of 'Regex' selecting type
		/// </summary>
		public string RegexValue { get; set; }

		/// <summary>
		/// XPath for selecting nodes
		/// </summary>
		public string XPathValue { get; set; }
	}

	public class MatchingRule
	{
		public string Name { get; set; }
		public MatchType Type { get; set; }
		public int MatchIndex { get; set; }
	}

	public enum SelectTypes
	{
		InnerHtml,
		AttributeValue,
		Constant,
		OriginalHtml,
		Marker,
		Regex,
		XPath
	}

	public enum MatchType
	{
		Direct,
		StartWith,
		EndWith,
		Contain,
		MatchAll
	}
}
